package xing.application.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import xing.domain.isv.suite.LoginUser;

import java.text.MessageFormat;

/*******************************************************************************************
 * 类描述：  
 *
 * @author: dustin
 * @date： 2017-8-31 18:45
 * @Description:
 * @version 1.0.0
 *
 *
 * Version    Date       ModifiedBy                 Content  
 * -------- ---------    ----------         ------------------------  
 * 1.0.0     2017-8-31    Administrator                            
 *******************************************************************************************
 */
@Service
public class CorpApi {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
    * @Description: 获取当前用户
    * @param:  * @param null
    * @Author: dustin
    * @Date: 2017-8-31
    */
    public LoginUser getCurrentUserByAuthCode(String suiteKey,String corpId, String accessToken,String code) throws Exception{
        String res = Client.getCommand("user/getuserinfo",
                MessageFormat.format("access_token={0}&code={1}",accessToken,code));

        JSONObject json = JSON.parseObject(res);
        if(json.getInteger("errcode")==0){
            return new LoginUser(corpId,json.getString("userid"),json.getBoolean("is_sys"),
                    json.getInteger("sys_level"),json.getString("deviceId"));
        } else{
            logger.error("获取当前用户信息失败:{}",res);
            throw new Exception(res);
        }
    }
}
